#!/usr/bin/env python
# -*- coding:utf-8 -*- 
import urllib2
import re
import pickle
from find_qq_pic import*
def get_img_url_lst(url):
	if 'online' in url:
		setp=30
	else:
		setp=18
	lst=[]
	flag=1	
	startid=0
	while flag:
		n_url=''.join([url,'?start=',str(startid)])
		print 'start a new work %s' %n_url 
		try:
			li,title=find_blog(n_url)
			print '------------%s' %len(li)
			lst.extend(li)
			if len(li)>0:
				startid+=setp
				print u'找到一组图片地址成功'
			else:
				flag=0
				print u'没找到任何图片地址'		
		except:
			flag=0
			print u'没找到任何图片地址'
	return map(lambda x: ('','',x[2].replace('thumb','photo')),lst),title
		
def find_blog(url):
    print 'start'
    cj=cookielib.LWPCookieJar()
    opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)
    headers={
'User-Agent':'Mozilla/5.0 Firefox/3.6.8',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'en-us,en;q=0.5',
'Accept-Encoding':'deflate',
'Accept-Charset':'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Keep-Alive':'115',
'Connection':'keep-alive',
'Cache-Control':'no-cache',}
    print 'start request url'
    req=urllib2.Request(url,headers=headers)
    html=opener.open(req).read()
    print len(html),'read html ok'
    title=re.findall('<title>([\s\S]*?)</title>',html)
    title=title[0]
    title=title.split('-')[1]
    title=title.strip()
    #title=re.sub('腾讯网','',title)
    lst_img_url=re.findall('<img src=\"(http://img\d.douban.com/view/photo/\S{5}/public/\S*?.jpg)\" />',html)
    #catalog=re.findall('rel\=\"category tag\">([\s\S]*?)</a>',html)[0]
    #tag=re.findall('rel\=\"tag\">([\s\S]*?)</a>',html)[0]
    lst_img=[]
    for i in lst_img_url:
        lst_img.append(('','',i))
    return lst_img,title

if __name__=='__main__':
	tag=raw_input('请输入标签：')
	catalog=raw_input('请输入目录：')
	url=raw_input('请输入豆瓣相册地址不带参数：')
	lst_img,title=get_img_url_lst(url)	
	name=''
	#try:
	data_site(lst_img,title,name,tag,catalog)
	#except:
	#	print 'proces is filed'
	#print '==========================================='
